package com.hmall.order.service.impl;

import ch.qos.logback.core.util.TimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.client.ItemClient;
import com.hmall.common.client.UserClient;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.RequestParams;
import com.hmall.order.service.IOrderService;
import com.hmall.order.utils.UserHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    // private IdWorker idWorker = new IdWorker(10L, 10L, 0L);

    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;
    @Autowired
    private OrderDetailService detailService;
    @Autowired
    private OrderLogisticsService logisticsService;

    @Transactional
    @Override
    public Long createOrder(RequestParams requestParams) {
        Order order = new Order();
        // 查询商品
        Item item = itemClient.queryItemById(requestParams.getItemId());
        // 基于商品价格、购买数量计算商品总价：totalFee
        long totalFee = item.getPrice() * requestParams.getNum();
        order.setTotalFee(totalFee);
        order.setPaymentType(requestParams.getPaymentType());
        order.setUserId(UserHolder.getUser());
        order.setStatus(1);
        // 将Order写入数据库tb_order表中
        this.save(order);

        // 将商品信息、orderId信息封装为OrderDetail对象，写入tb_order_detail表
        OrderDetail detail = new OrderDetail();
        detail.setName(item.getName());
        detail.setSpec(item.getSpec());
        detail.setPrice(item.getPrice());
        detail.setNum(requestParams.getNum());
        detail.setItemId(item.getId());
        detail.setImage(item.getImage());
        detail.setOrderId(order.getId());
        detailService.save(detail);


        Long addressId = requestParams.getAddressId();
        // 根据addressId查询user-service服务，获取地址信息
        Address address = userClient.findAddressById(addressId);
        // 将地址封装为OrderLogistics对象，写入tb_order_logistics表
        OrderLogistics orderLogistics = new OrderLogistics();
        BeanUtils.copyProperties(address, orderLogistics);
        orderLogistics.setOrderId(order.getId());
        logisticsService.save(orderLogistics);

        // 7.扣减库存
        try {
            itemClient.updateStock(requestParams.getItemId(), requestParams.getNum());
        } catch (Exception e) {
            throw new RuntimeException("库存不足！");
        }
        return order.getId();
    }

    @Override
    public void closeTimeoutOrder() {
        // 订单状态是1（未付款） 并且 当前时间-下单时间大于等于两个小时的
        // select od.* from tb_order o,tb_order_detail od
        // where o.id=od.order_id and  o.status = 1
        // and TIMESTAMPDIFF(MINUTE,o.create_time,DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S'))>=30
        List<OrderDetail> orderDetailList = detailService.getBaseMapper().queryTimeoutOrderDetail();
        if (orderDetailList == null || orderDetailList.size() == 0) {
            return;
        }
        // 恢复库存
        for (OrderDetail orderDetail : orderDetailList) {
            itemClient.updateStock(orderDetail.getItemId(), -orderDetail.getNum());
        }
        // 收集订单id
        List<Long> orderIdList = orderDetailList.stream().map(OrderDetail::getOrderId).collect(Collectors.toList());

        //  把满足以上条件的订单状态修改成 5（订单取消）
        LambdaUpdateWrapper<Order> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Order::getStatus, 5);
        updateWrapper.set(Order::getCloseTime, new Date());
        updateWrapper.in(Order::getId, orderIdList);
        this.update(updateWrapper);
        System.out.println(" 执行了定时任务....");
    }
}
